<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>申购单列表---申请人</title>
    <script type="text/javascript">
      var purchaseId;
      var purchaseDS;
      //打开窗口，准备发布。
      function openPurchaseWindow(id,searchType){
        purchaseForAuditorWin = new Ext.Window({
          layout: 'fit',
          width: 780,
          height: 475,
          closeAction: 'destroy',
          plain: true,
          modal: true,
          constrain:true,
          //modal: true,
          title: '申购单详细信息',
          autoScroll:true,
          autoLoad: {
            url: "/purchase/purchaseForAuditor?search_type="+searchType + "&id=" + id,
            scripts: true
          }
        });
    
        purchaseForAuditorWin.on("destroy",function(){
          purchaseDS.reload();
        });
        purchaseForAuditorWin.resizable = false;
        purchaseForAuditorWin.show();
        if(searchType === "audited"){
          purchaseForAuditorWin.setHeight(362);
        }else if(searchType === "released" || searchType === "for_entry" || searchType === "for_account" ||searchType === "primary"||searchType === "pass"||searchType === "complete"||searchType === "failure_to_acceptance"){
          purchaseForAuditorWin.setHeight(510);
        }else if(searchType === "failure" || searchType === "terminated" || searchType === "auditprimary"  || searchType === "reject"){
          purchaseForAuditorWin.setHeight(510);
        }
      }
        
      Ext.onReady(function(){
        //searchForm
        var searchType = new Ext.form.Hidden({
          name: 'search_type',
          value: '<%=params[:search_type]%>'
        });
        
        var purchaseNo = new Ext.form.TextField({
          fieldLabel: '申购单号',
          allowBlank: true,
          blankText: "申购单位不能为空",
          name: 'order_number',
          anchor: '96%'
        });

        var productName = new Ext.form.TextField({
          fieldLabel: '产品名称',
          allowBlank: true,
          blankText: "产品名称",
          name: 'product_name',
          anchor: '96%'
        });

        var fundingAccount = new Ext.form.TextField({
          fieldLabel: '经费账号',
          name: 'funding_account',
          anchor: '90%'
        });

        var unitPriceFrom = new Ext.form.NumberField({
          fieldLabel: '单价(从)',
          name: 'unit_price_from',
          allowBlank: true,
          allowDecimals: false,
          allowNegative: false,
          anchor: '95%'
        });

        var unitPriceTo = new Ext.form.NumberField({
          fieldLabel: '到',
          name: 'unit_price_to',
          allowBlank: true,
          allowDecimals: false,
          allowNegative: false,
          anchor: '95%'
        });

        var amountFrom = new Ext.form.NumberField({
          fieldLabel: '总额(从)',
          name: 'amount_from',
          allowBlank: true,
          allowDecimals: false,
          allowNegative: false,
          anchor: '95%'
        });

        var amountTo = new Ext.form.NumberField({
          fieldLabel: '到',
          name: 'amount_to',
          allowBlank: true,
          allowDecimals: false,
          allowNegative: false,
          anchor: '95%'
        });
        
        //搜索按钮
        var searchButton = new Ext.Button({
          text: '搜索',
          tooltip: '搜索申购记录',
          iconCls: 'finding',
          handler: function(){
            purchaseDS.baseParams.search_type = searchType.getValue();
            purchaseDS.baseParams.product_name = productName.getValue();
            purchaseDS.baseParams.order_number = purchaseNo.getValue();
            purchaseDS.baseParams.funding_account = fundingAccount.getValue();
            purchaseDS.baseParams.unit_price_from = unitPriceFrom.getValue();
            purchaseDS.baseParams.unit_price_to = unitPriceTo.getValue();
            purchaseDS.baseParams.amount_from = amountFrom.getValue();
            purchaseDS.baseParams.amount_to = amountTo.getValue();            
            purchaseDS.load({
              params: {
                start: 0,
                limit: 15
              }
            });
          }
        })
        
        var searchPurchaseForm = new Ext.FormPanel({
          labelWidth: 55, // label settings here cascade unless overridden
          url:'/purchase/create',
          frame:true,
          labelAlign: 'right',
          buttonAlign: 'right',
          //title: '添加申购单',
          bodyStyle:'padding:5px 5px 0',
          baseCls:'x-panel-noborder',
          border:false,
          width: 800,
          //height:300,
          items: [
            {
              xtype: 'fieldset',
              defaults: {width: 750},
              title: '条件搜索',
              border: true,
              items: [
                {
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [purchaseNo]
                    }, {
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [productName]
                    }, {
                      columnWidth: .3,
                      layout: 'form',
                      border: false,
                      items: [fundingAccount]
                    }]
                },{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [unitPriceFrom]
                    },{
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [unitPriceTo]
                    },{
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [amountFrom]
                    },{
                      columnWidth: .15,
                      layout: 'form',
                      border: false,
                      items: [amountTo]
                    },{
                      columnWidth: .2,
                      layout: 'form',
                      border: false,
                      items: [searchType]
                    },{
                      columnWidth: .2,
                      layout: 'form',
                      border: false,
                      items: [searchButton]
                    }]
                }]
            }]
        });
        //申购列表---------------------------------------------------------
        //生成有关申购的Grid
        purchaseDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: 15
          },
          proxy: new Ext.data.HttpProxy({
            url: '/purchase/getPurchasesForAuditor?search_type=<%=params[:search_type]%>'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'manager'
            }, {
              name: 'order_number'
            }, {
              name: 'product_name'
            }, {
              name: 'model'
            }, {
              name: 'specification'
            }, {
              name: 'manufacturer'
            }, {
              name: 'estimated_price'
            }, {
              name: 'estimated_amount'
            }, {
              name: 'auditing_status'
            }, {
              name: 'release_status'
            }, {
              name: 'offers_count'
            }, {
              name: 'brand'
            }, {
              name: 'begin_date'
            }, {
              name: 'end_date'
            }, {
              name: 'is_primary'
            }, {
              name: 'force_primary'
            }, {
              name: 'primary_status'
            },{
              name: 'created_at'
            }])
        });
        var purchaseSM = new Ext.grid.CheckboxSelectionModel();
        var purchaseCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),purchaseSM,{
            header: '申购序号',
            dataIndex: 'order_number',
            width:130
          }, {
            header: '产品名称',
            dataIndex: 'product_name',
            width:110
          }, {
            header: '品牌',
            dataIndex: 'brand',
            width:50
          }, {
            header: '型号规格',
            dataIndex: 'specification',
            width:80
          }, {
            header: '生产商',
            dataIndex: 'manufacturer',
            width:80
          }, {
            header: '预估单价',
            dataIndex: 'estimated_price',
            width:60
          }, {
            header: '预估总额',
            dataIndex: 'estimated_amount',
            width:60
          }, {
            header: '审批状态',
            dataIndex: 'auditing_status',
            renderer:auditingRender,
            width:60
          }, {
            header: '发布状态',
            dataIndex: 'release_status',
            renderer: releaseRender,
            width:60
          }, {
            header: '供应商',
            dataIndex: 'offers_count',
            width:50
          }, {
            header: '竞开日期',
            dataIndex: 'begin_date',
            renderer: Ext.util.Format.dateRenderer('Y-m-d'),
            width:70
          }, {
            header: '竞束日期',
            dataIndex: 'end_date',
            renderer: Ext.util.Format.dateRenderer('Y-m-d'),
            width:70
          }, {
            header: '申购时间',
            dataIndex: 'created_at',
            renderer: Ext.util.Format.dateRenderer('m-d H:i'),
            width:70
          }, {
            header: '已初选',
            dataIndex: 'is_primary',
            width:50
          }, {
            header: '初选<br>指定',
            dataIndex: 'force_primary',
            width:40
          }, {
            header: '初选<br>审批',
            dataIndex: 'primary_status',
            width:40
          }, {
            header: '操作',
            dataIndex: 'manufacturer',
            renderer: actionRender,
            width:70
          }]);

        function actionRender(value, cellmeta, record, rowIndex, columnIndex, store){
          var actionName = "";
          if(searchType.getValue() === "all"){
            return
          }else if(searchType.getValue() === "audited"){
            actionName = '审批发布';
          }else if(searchType.getValue() === "primary" || searchType.getValue() === "pass" || searchType.getValue() === "failure_to_acceptance" || searchType.getValue() === "complete"|| searchType.getValue() === "for_entry"|| searchType.getValue() === "for_account"){
            actionName = '查看';
          }else if( searchType.getValue() === "failure" || searchType.getValue() === "terminated" || searchType.getValue() === "released" || searchType.getValue() === "auditprimary" || searchType.getValue() === "reject"){
            return "<a style=cursor:pointer;color:blue onclick=openPurchaseWindow(" + store.getAt(rowIndex).get('id')+",'"+searchType.getValue()+"')>查看</a>";
          }
          return "<a style=cursor:pointer;color:blue onclick=openPurchaseWindow(" + store.getAt(rowIndex).get('id')+",'"+searchType.getValue()+"')>" + actionName + "</a>";
        }

        function releaseRender(value, cellmeta, record, rowIndex, columnIndex, store){
          var statusColor;
          var statusValue = store.getAt(rowIndex).get('release_status');
          if (statusValue === "未发布"){
            statusColor = 'red';
          }else if (statusValue === "已发布"){
            statusColor = 'green';
          }
          return "<font color="+statusColor+">"+statusValue+"</font>"
        }
        
        var purchaseGrid =  new Ext.grid.GridPanel({
          ds: purchaseDS,
          cm: purchaseCM,
          sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
          width: screenWidth-190,
          height: screenHeight-440,
          iconCls: 'icon-grid',loadMask: true,
          loadMask: true,
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: purchaseDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })
        });
        purchaseDS.load();

        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-180,
          height: screenHeight-260,
          //html: '&lt;empty panel&gt;',
          //cls:'cellpadding:10',
          /*layout: {
            type:'vbox',
            align:'stretch'
          },*/
          frame:true,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          //bodyStyle: 'padding:15px;align:center',
          //listeners: {
          //expand: showAgileDiagram
          //},
          items:[searchPurchaseForm,purchaseGrid]
        });
        workbenchPanel.render("workbench_panel");
      })
    </script>
  </head>
  <body>
    <div id="search_form"></div>
    <div id="purchase_list"></div>
    <div id="workbench_panel"></div>
  </body>
</html>
